<div class="bjui-pageHeader">
    <form id="pagerForm" data-toggle="ajaxsearch" action="{:U('invoiceIndex')}" method="post">
        <div class="bjui-searchBar">
            <div class="bjui-searchBar">
                <label>条件搜索：</label>

                <div role="group" class="btn-group">
                    <input type="text" name="value" class="form-control" size="20" value="{:I('value')}">
                    <select name="key" data-toggle="selectpicker">
                        <option value="">全部</option>
                        <option value="order_no" <?php echo I("key")=="order_no" ? "selected='true'" : "";?>>系统订单号</option>
                        <option value="order_sn" <?php echo I("key")=="order_sn" ? "selected='true'" : "";?>>B2C网站订单号</option>
                        <option value="user_name" <?php echo I("key")=="user_name" ? "selected='true'" : "";?>>姓名</option>
                        <option value="mobile_phone" <?php echo I("key")=="mobile_phone" ? "selected='true'" : "";?>>手机</option>
                        <option value="home_phone" <?php echo I("key")=="home_phone" ? "selected='true'" : "";?>>电话</option>
                        <option value="address" <?php echo I("key")=="address" ? "selected='true'" : "";?>>地址</option>
                        <option value="goods_sn" <?php echo I("key")=="goods_sn" ? "selected='true'" : "";?>>货号</option>
                        <option value="bar_code" <?php echo I("key")=="bar_code" ? "selected='true'" : "";?>>条码</option>
                        <option value="seller_note" <?php echo I("key")=="seller_note" ? "selected='true'" : "";?>>卖家备注</option>
                        <option value="message" <?php echo I("key")=="message" ? "selected='true'" : "";?>>买家留言</option>
                        <option value="check_message" <?php echo I("key")=="check_message" ? "selected='true'" : "";?>>审单备注</option>
                    </select>
                </div>
<!--                 &nbsp;
                <label>银行类型：</label>

                <div role="group" class="btn-group">
                    <select name="bank_id" data-toggle="selectpicker">
                        <option value="">全部</option>
                        <?php foreach($bankList as $bank):?>
                        <option value="{$bank.id}" <?php echo I("bank_id")==$bank['id'] ? "selected='true'" : "";?>>{$bank.name}</option>
                        <?php endforeach;?>
                    </select>
                </div> -->
                &nbsp;
                <button type="submit" class="btn-default" data-icon="search">查询</button>&nbsp;
                <a class="btn btn-orange" href="javascript:;" onclick="$(this).navtab('reloadForm', true)" data-icon="undo">清空查询</a>&nbsp;
                <!-- <div class="alert alert-info search-inline"><i class="fa fa-info-circle"></i> 双击行可编辑</div>&nbsp; -->
<!--                 <div class="pull-right">
                    <div class="btn-group">
                        <button type="button" class="btn-default dropdown-toggle" data-toggle="dropdown" data-icon="copy">批量操作<span class="caret"></span></button>
                        <ul class="dropdown-menu right" role="menu">
                            <li><a href="{:U('createOrder')}/orderIds/{#bjui-selected}" data-toggle="doajax" data-confirm-msg="确定要生成选中项订单吗？">生成订单</a></li>
                        </ul>
                    </div>
                </div> -->
            </div>
        </div>
    </form>
</div>
<div class="bjui-pageContent tableContent">
        <table class="table table-bordered table-hover table-striped table-top" data-selected-multi="true">
        <thead>
        <tr>
            <th><input type="checkbox" class="checkboxCtrl" data-group="ids" data-toggle="icheck">选择</th>
            <th data-order-field="order_no" align="center">系统订单号</th>
            <th data-order-field="order_sn" align="center">网店单号</th>
            <th>发票类型</th>
            <th>发票抬头</th>
            <th>发票号码 <label class="control-label"><input type="checkbox" class="checkboxCtrl"
                                                         name="consecutive"
                                                         data-toggle="icheck">连号</label></th>
        </tr>
        </thead>
        <tbody>
        <?php foreach($list as $arr):?>
        <tr>
            <td><input type="checkbox" name="ids" data-toggle="icheck" value="{$arr.order_id}"></td>
            <!-- 系统订单号 -->                    
            <td>
                <a  data-toggle="navtab" data-options="{id:'Order{$arr['order_id']}', url:'{:U('order/detailsOrder?orderId='.$arr['order_id'].'&isPrinter=1'.'&navTabId=Order'.$arr['order_id'])}', title:'{$arr.order_sn}-订单详细'}">{$arr.order_no}</a>
            </td>
            <!-- 网店单号 -->                    
            <td>{$arr.order_sn}</td>
            <!-- 发票类型 -->
            <td>{$arr.invoice_type}</td>
            <!-- 发票抬头 -->
            <td>{$arr.invoice_title}</td>
            <td>
                <form action="{:U('addInvoiceSn')}" class="pageForm" data-toggle="validate" method="post">
                    <!--<input type="hidden" name="invoice_id" value="{$arr.invoice_id}">-->
                    <input type="hidden" name="order_id" value="{$arr.order_id}">
                    <input type="text" name="invoice_sn" value="{$arr.invoice_sn}" data-rule="required,express"
                                          data-rule-express="[/^\*\d/, '当前快递单号不符合规则']" />            
                    <button type="submit" class="btn-green">添加发票号码</button>
                </form>
            </td>
        </tr>
        <?php endforeach;?>
        </tbody>
    </table>
</div>
<div class="bjui-pageFooter">
    <ul>
        <input type="checkbox" class="checkboxCtrl" data-group="ids" data-toggle="icheck">
        <a id="batchAddInvoiceSn" class="btn btn-default" href="javascript:;" data-icon="save">批量添加发票号码</a>
    </ul>
    <div class="pages"><span>每页&nbsp;</span>

        <div class="selectPagesize">
            <select data-toggle="selectpicker" data-toggle-change="changepagesize">
                <option value="{$pageSize}">{$pageSize}</option>
                <option value="{$pageSize*3}">{$pageSize*3}</option>
                <option value="{$pageSize*6}">{$pageSize*6}</option>
                <option value="{$pageSize*12}">{$pageSize*12}</option>
                <option value="{$pageSize*15}">{$pageSize*15}</option>
            </select>
        </div>
        <span>&nbsp;条，共 {$total} 条</span></div>
    <div class="pagination-box" data-toggle="pagination" data-total="{$total}" data-page-size="{$pageSize}"
         data-page-current="{$pageCurrent}"></div>
</div>

<script>
    
/**
 * [批量添加发票号码]
 * @param  {[type]} ){} [description]
 * @return {[type]}       [description]
 */
$.CurrentNavtab.find("#batchAddInvoiceSn").click(function(){
    var consecutiveList = $.CurrentNavtab.find("input[type=checkbox][name=ids]:checked").parent().parent().parent().find("form");

    if (consecutiveList.length == 0) {
        $(this).alertmsg('info', '当前没有选项', {title: '提示'})
        $.CurrentNavtab.find("input[type=checkbox][name=consecutive]").iCheck('uncheck');
        return false;
    }

    var options = {};
    options['url'] = "{:U('addInvoiceSn')}";
    options['data'] = {};
    options['data']['list'] = {};
    options['type'] = 'POST';
    options['dataType'] = 'json';

    $.each(consecutiveList, function(index){
        var orderId = $(this).val();
        var _form = $(this);
        console.log(_form.isValid());
        if(_form.isValid()){
            var formSerializeArray = _form.serializeArray();
            $.each(formSerializeArray, function(i, input){
                options['data']['list'][index] = options['data']['list'][index] || {};
                options['data']['list'][index][input['name']] = input['value'];
            })
        }
    })


    options['success'] = function(result){
        console.log(result);
        if(result && result[BJUI.keys.statusCode]==BJUI.statusCode.ok){
            $.CurrentNavtab.navtab('reload');          // 刷新当前窗口
            $(this).alertmsg('ok', result[BJUI.keys.message]);
        }else if(result && result[BJUI.keys.statusCode]==BJUI.statusCode.both){
            $.CurrentNavtab.navtab('reload');          // 刷新当前窗口
            $(this).alertmsg('warn', result[BJUI.keys.message], {alertTimeout:30000});
        }else if(result && result[BJUI.keys.statusCode]==BJUI.statusCode.error){
            $(this).alertmsg('error', result[BJUI.keys.message]);
        }else{
            $(this).alertmsg('error', '没有数据返回, 请重新提交数据');
        }
    }
    console.log(options);
    if(!$.isEmptyObject(options['data'])){
        $.ajax(options)
    }

})

// console.log($.CurrentNavtab.find("input[type=checkbox][name=consecutive]").length);

/**
 * [连号操作]
 * @param  {[type]} ) {}          [description]
 * @return {[type]}   [description]
 */
$.CurrentNavtab.find("input[type=checkbox][name=consecutive]").on('ifChecked',function () {
    var consecutiveList = $.CurrentNavtab.find("input[type=checkbox][name=ids]:checked").parent().parent().parent().find("input[type=text][name=invoice_sn]");

    if (consecutiveList.length == 0) {
        $(this).alertmsg('info', '当前没有选项', {title: '提示'})
        $.CurrentNavtab.find("input[type=checkbox][name=consecutive]").iCheck('uncheck');
        return false;
    }

    if (typeof consecutiveList == 'undefined' || consecutiveList[0].value == "") {
        $(this).alertmsg('info', '请输入第一个发票号码', {title: '提示'})
        consecutiveList[0].focus();
        $.CurrentNavtab.find("input[type=checkbox][name=consecutive]").iCheck('uncheck');
        return false;
    }


    var invoiceSn = Number(consecutiveList[0].value.substr(1));
    $(consecutiveList).each(function(i){
        invoiceSn++;
        $(this).val('*'+invoiceSn);
    })


}).on('ifUnchecked', function () {
    var consecutiveList = $.CurrentNavtab.find("input[type=checkbox][name=ids]:checked").parent().parent().parent().find("input[type=text][name=invoice_sn]");
    var consecutiveContent = typeof consecutiveList[0] != 'undefined' ? consecutiveList[0].value : '';
    consecutiveList.val('');    // 情况快递单号
    if (consecutiveContent) {
        consecutiveList[0].value = consecutiveContent;
    }
})


/**
 *  选择复选框效果

$.CurrentNavtab.find("input[type=checkbox][name=ids]").on('ifChecked',function () {
    $(this).parent().parent().parent().find("input[name!=ids]").attr("disabled", false);
}).on('ifUnchecked', function () {
    $(this).parent().parent().parent().find("input[name!=ids]").attr("disabled", true);
})
 */


</script>